<template>
  <Dialog
    v-model="dialogVisible"
    :max-height="500"
    :scroll="true"
    title="详情"
    class="enhanced-dialog"
  >
    <!-- 弹窗头部 -->
    <div class="dialog-header">
      <div class="header-icon">
        <Icon icon="ep:view" />
      </div>
      <div class="header-content">
        <h4 class="dialog-title">任务详情</h4>
        <p class="dialog-subtitle">查看任务的详细信息</p>
      </div>
    </div>

    <!-- 详情内容 -->
    <div class="detail-container">
      <el-descriptions :column="1" border class="enhanced-descriptions">
        <el-descriptions-item label="任务编号" min-width="120" class="desc-item">
          <div class="desc-value id-value">
            <Icon icon="ep:key" class="value-icon" />
            <span class="value-text">{{ detailData.id }}</span>
          </div>
        </el-descriptions-item>

        <el-descriptions-item label="任务名称" class="desc-item">
          <div class="desc-value name-value">
            <Icon icon="ep:finished" class="value-icon" />
            <span class="value-text">{{ detailData.name }}</span>
          </div>
        </el-descriptions-item>

        <el-descriptions-item label="所属流程" class="desc-item">
          <div class="desc-value process-value">
            <Icon icon="ep:grid" class="value-icon" />
            <span class="value-text">{{ detailData.processInstance?.name || '-' }}</span>
          </div>
        </el-descriptions-item>

        <el-descriptions-item label="流程发起人" class="desc-item">
          <div class="desc-value user-value">
            <Icon icon="ep:user" class="value-icon" />
            <span class="value-text">{{
              detailData.processInstance?.startUserNickname || '-'
            }}</span>
          </div>
        </el-descriptions-item>

        <el-descriptions-item label="状态" class="desc-item">
          <div class="desc-value status-value">
            <Icon icon="ep:switch" class="value-icon" />
            <dict-tag
              :type="DICT_TYPE.BPM_PROCESS_INSTANCE_RESULT"
              :value="detailData.result"
              class="enhanced-dict-tag"
            />
          </div>
        </el-descriptions-item>

        <el-descriptions-item label="原因" class="desc-item">
          <div class="desc-value reason-value">
            <Icon icon="ep:chat-line-round" class="value-icon" />
            <span class="value-text reason-text">{{ detailData.reason || '-' }}</span>
          </div>
        </el-descriptions-item>

        <el-descriptions-item label="创建时间" class="desc-item">
          <div class="desc-value time-value">
            <Icon icon="ep:clock" class="value-icon" />
            <span class="value-text">{{ formatDate(detailData.createTime) }}</span>
          </div>
        </el-descriptions-item>
      </el-descriptions>
    </div>
  </Dialog>
</template>

<script lang="ts" setup>
import { DICT_TYPE } from '@/utils/dict'
import { formatDate } from '@/utils/formatTime'
import * as TaskApi from '@/api/bpm/task'

defineOptions({ name: 'BpmTaskDetail' })

const dialogVisible = ref(false) // 弹窗的是否展示
const detailLoading = ref(false) // 表单的加载中
const detailData = ref() // 详情数据

/** 打开弹窗 */
const open = async (data: TaskApi.TaskVO) => {
  dialogVisible.value = true
  // 设置数据
  detailLoading.value = true
  try {
    detailData.value = data
  } finally {
    detailLoading.value = false
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>

<style lang="scss" scoped>
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$info-color: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

/* 弹窗样式 */
.enhanced-dialog {
  :deep(.el-dialog) {
    border-radius: $border-radius;
    overflow: hidden;

    @media (max-width: 768px) {
      width: 95% !important;
      margin: 0 auto;
    }
  }

  :deep(.el-dialog__header) {
    padding: 0;
    border-bottom: none;
  }

  :deep(.el-dialog__body) {
    padding: 0;
  }

  :deep(.el-dialog__headerbtn) {
    top: 16px;
    right: 16px;
    z-index: 10;

    .el-dialog__close {
      color: white;
      font-size: 18px;

      &:hover {
        color: rgba(255, 255, 255, 0.8);
      }
    }
  }
}

/* 弹窗头部 */
.dialog-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }
}

.header-icon {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 36px;
    height: 36px;
    font-size: 16px;
  }
}

.header-content {
  flex: 1;
  margin-left: 12px;

  @media (max-width: 768px) {
    margin-left: 0;
  }
}

.dialog-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.dialog-subtitle {
  font-size: 13px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 详情容器 */
.detail-container {
  padding: 24px;
  background: white;
  max-height: 60vh;
  overflow-y: auto;

  @media (max-width: 768px) {
    padding: 16px;
    max-height: 50vh;
  }

  /* 滚动条美化 */
  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.05);
    border-radius: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 82, 204, 0.3);
    border-radius: 3px;
    transition: all 0.3s ease;

    &:hover {
      background: rgba(0, 82, 204, 0.5);
    }
  }
}

/* 描述列表样式 */
.enhanced-descriptions {
  :deep(.el-descriptions__header) {
    margin-bottom: 20px;
  }

  :deep(.el-descriptions__table) {
    border-radius: $border-radius-small;
    overflow: hidden;
    box-shadow: $shadow-base;
  }

  :deep(.el-descriptions__cell) {
    padding: 16px 20px;
    border-color: $border-light;
    transition: all 0.3s ease;

    @media (max-width: 768px) {
      padding: 12px 16px;
    }

    &.is-bordered-label {
      background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%);
      color: $text-primary;
      font-weight: 600;
      font-size: 14px;
      width: 120px;
      min-width: 120px;

      @media (max-width: 768px) {
        font-size: 13px;
        width: 100px;
        min-width: 100px;
      }
    }

    &.is-bordered-content {
      background: white;

      &:hover {
        background: $primary-light;
      }
    }
  }

  // H5适配：垂直布局
  @media (max-width: 480px) {
    :deep(.el-descriptions__cell) {
      display: block !important;
      width: 100% !important;

      &.is-bordered-label {
        border-bottom: none !important;
        padding-bottom: 8px;
      }

      &.is-bordered-content {
        border-top: none !important;
        padding-top: 8px;
      }
    }
  }
}

.desc-item {
  :deep(.el-descriptions__label) {
    display: flex;
    align-items: center;
    gap: 8px;
  }
}

/* 描述值样式 */
.desc-value {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 24px;

  @media (max-width: 768px) {
    gap: 6px;
  }
}

.value-icon {
  font-size: 16px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    font-size: 14px;
  }
}

.value-text {
  flex: 1;
  color: $text-primary;
  font-weight: 500;
  line-height: 1.4;

  @media (max-width: 768px) {
    font-size: 13px;
  }
}

/* 不同类型值的图标颜色 */
.id-value .value-icon {
  color: $primary-color;
}

.name-value .value-icon {
  color: $success-color;
}

.process-value .value-icon {
  color: $primary-color;
}

.user-value .value-icon {
  color: $info-color;
}

.status-value .value-icon {
  color: $warning-color;
}

.reason-value .value-icon {
  color: $warning-color;
}

.time-value .value-icon {
  color: $text-secondary;
}

/* 特殊值样式 */
.id-value .value-text {
  font-family: 'Courier New', monospace;
  font-size: 12px;
  word-break: break-all;

  @media (max-width: 768px) {
    font-size: 11px;
  }
}

.reason-text {
  line-height: 1.5;
  max-height: 60px;
  overflow-y: auto;
  word-break: break-word;

  @media (max-width: 768px) {
    max-height: 50px;
  }

  /* 滚动条美化 */
  &::-webkit-scrollbar {
    width: 4px;
  }

  &::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.05);
    border-radius: 2px;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 82, 204, 0.3);
    border-radius: 2px;
  }
}

/* 字典标签样式 */
.enhanced-dict-tag {
  border-radius: 6px;
  font-weight: 500;
  padding: 4px 8px;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 4px;

  @media (max-width: 768px) {
    font-size: 11px;
    padding: 3px 6px;
  }

  &::before {
    content: '';
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: currentColor;
    opacity: 0.8;
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .detail-container {
    background: #374151;
  }

  .enhanced-descriptions {
    :deep(.el-descriptions__cell) {
      border-color: #4b5563;

      &.is-bordered-label {
        background: #4b5563;
        color: #f9fafb;
      }

      &.is-bordered-content {
        background: #374151;

        &:hover {
          background: rgba(96, 165, 250, 0.1);
        }
      }
    }
  }

  .value-text {
    color: #f9fafb;
  }

  .reason-text {
    color: #d1d5db;
  }

  .id-value .value-text {
    color: #f9fafb;
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .dialog-header {
    padding: 12px 16px;
  }

  .detail-container {
    padding: 12px;
  }

  .enhanced-descriptions {
    :deep(.el-descriptions__cell) {
      padding: 10px 12px;
    }
  }

  .desc-value {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }

  .value-icon {
    align-self: flex-start;
  }
}
</style>
